import request from "../../utils/request"

let startY = 0;
let moveDistance = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    translateY: 'translateY()',
    transition: '',
    userInfo: {},
    recentPlayList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({
        userInfo: JSON.parse(userInfo)
      })
      this.getUserRecordList()
    }
  },
  // 手指按下
  handleTouchStart(e) {
    this.setData({
      transition: ''
    })
    startY = e.touches[0].clientY;
  },
  // 手指移动
  handleTouchMove(e) {
    moveDistance = e.touches[0].clientY - startY;
    if (moveDistance <= 0) {
      return;
    }
    if (moveDistance >= 80) {
      moveDistance = 80
    }
    this.setData({
      translateY: `translateY(${moveDistance}rpx)`
    })
  },
  // 手指抬起
  handleTouchEnd() {
    // console.log('end');
    this.setData({
      translateY: `translateY(0rpx)`,
      transition: 'transform 1s linear'
    })
  },
  // 获取播放记录
  async getUserRecordList() {
    let recentPlayList = await request('user/record', {
      uid: this.data.userInfo.userId,
      type: 0
    });
    if (recentPlayList.allData.length > 5) {
      recentPlayList = recentPlayList.allData.slice(0, 5).map((item, index) => {
        item.id = index;
        return item;
      });

    }
    this.setData({
      recentPlayList
    })
  },
  // 跳转登录页面
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})